package com.lsh.airticket.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dto.RequestFlight;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Flight;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.FlightService;
import com.lsh.airticket.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lsh
 * @date 2021/2/17
 **/
@RestController
@RequestMapping("/flights")
public class AdminFlightController {

    @Autowired
    FlightService flightService;

    /**
     * 分页查找
     * @param page
     * @return
     */
    @GetMapping("/admin/flight/flightPage")
    public Result findFlightPage(Integer page) {
        PageHelper.startPage(page, Constants.PAGE_SIZE);
        PageInfo<Flight> pageInfo= flightService.findFlightPage();
        return Result.result().addData("pageInfo", pageInfo);
    }

    /**
     * 插入一条数据

     */
    @PostMapping("/admin/flight/insertFlight")
    public Result insertFlight(@RequestBody RequestFlight requestFlight) {
        System.out.println(requestFlight);
        int i = flightService.insertFlight(requestFlight);
        if (i != 1) {
            throw new UserException("添加失败");
        }
        return Result.result();
    }
    /**
     * 根据主键删除一条数据
     */
    @GetMapping("/admin/flight/deleteFlight")
    public Result deleteFlight(String flightId) {
        int delete = flightService.deleteFlightById(flightId);
        if (delete != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }

    /**
     * 根据航班的id查找航班
     */
    @GetMapping("/admin/flight/findFlightById")
    public Result findFlightById(String flightId) {
        Flight flight = flightService.findFlightById(flightId);
        return Result.result().addData("flight", flight);
    }

    /**
     * 插入一条数据

     */
    @PostMapping("/admin/flight/updateFlight")
    public Result updateFlight(@RequestBody RequestFlight requestFlight) {
        System.out.println(requestFlight);
        int i = flightService.updateFlight(requestFlight);
        if (i != 1) {
            throw new UserException("更新失败");
        }
        return Result.result().setMsg("更新成功");
    }
}
